#include "thread.h"

int changeDir(int netfd,sharInfo_t *sharInfo,MYSQL*mysql){

    int praID=sharInfo->parentID;
    char path[256];
    bzero(path,sizeof(path));

    train_t rectrain;
    train_t sendtrain;
    bzero(&rectrain,sizeof(rectrain));
    bzero(&sendtrain,sizeof(rectrain));

    recv(netfd,&rectrain.length,sizeof(int),MSG_WAITALL);
    recv(netfd,rectrain.buf,rectrain.length,MSG_WAITALL);

    if(strcmp(rectrain.buf,"..")==0||strcmp(rectrain.buf,"../")==0){
        if(sharInfo->parentID==-1){
            sprintf(sendtrain.buf,"%s/",sharInfo->user);
            sendtrain.length=strlen(sendtrain.buf);

            send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
            send(netfd,sendtrain.buf,sendtrain.length,MSG_NOSIGNAL);
        }       
        sharInfo->lastID=sharInfo->parentID;
        get_grandpaID(sharInfo,mysql);
        get_pwd(sharInfo,mysql,sendtrain.buf);
        sendtrain.length=strlen(sendtrain.buf);

        send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
        send(netfd,sendtrain.buf,sendtrain.length,MSG_NOSIGNAL);
        return 0;
    }

    else if(strcmp(rectrain.buf,"~")==0||strcmp(rectrain.buf,"~/")==0){
        sharInfo->lastID=sharInfo->parentID;
        sharInfo->parentID=-1;
        sprintf(sendtrain.buf,"%s/",sharInfo->user);
        sendtrain.length=strlen(sendtrain.buf);

        send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
        send(netfd,sendtrain.buf,sendtrain.length,MSG_NOSIGNAL);
        return 0;

    }

    else if(strcmp(rectrain.buf,"-")==0){
        int tmep=sharInfo->parentID;
        sharInfo->parentID=sharInfo->lastID;
        sharInfo->lastID=tmep;

        get_pwd(sharInfo,mysql,sendtrain.buf);
        sendtrain.length=strlen(sendtrain.buf);

        send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
        send(netfd,sendtrain.buf,sendtrain.length,MSG_NOSIGNAL);
        return 0;
    }
    else{

        char *word=(char*)malloc(256*sizeof(char));
        char *saveptr=(char*)malloc(256);
        bzero(word,sizeof(word));
        bzero(saveptr,sizeof(saveptr));

        word=strtok_r(rectrain.buf,"/",&saveptr);
        if(strcmp(word,"~")==0){
            sharInfo->parentID=-1;
        }
        else if(strcmp(word,"..")==0){
            if(sharInfo->parentID==-1){
                sprintf(sendtrain.buf,"%s/",sharInfo->user);
                sendtrain.length=strlen(sendtrain.buf);

                send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
                send(netfd,sendtrain.buf,sendtrain.length,MSG_NOSIGNAL);

                return 0;
            }

        }

        int goalID=-2;
        int chret=checkDir(sharInfo,mysql,word,&goalID);
        if(chret==-1){
            sharInfo->parentID=praID;
            sendtrain.length=-1;
            send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
            printf("chret = -1\n");
            return 0;
        }
        sharInfo->parentID=goalID;
        while(1){
            word=strtok_r(NULL,"/",&saveptr);
            if(!word){
                break;
            }
            chret=checkDir(sharInfo,mysql,word,&goalID);
            if(chret==-1){
                sharInfo->parentID=praID;
                sendtrain.length=-1;
                send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
                return 0;
            }
            sharInfo->parentID=goalID;
        }
        sharInfo->lastID=praID;
        get_pwd(sharInfo,mysql,sendtrain.buf);
        sendtrain.length=strlen(sendtrain.buf);

        send(netfd,&sendtrain.length,sizeof(int),MSG_NOSIGNAL);
        send(netfd,sendtrain.buf,sendtrain.length,MSG_NOSIGNAL);

        free(word);
        return 0;

    }
}
